<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//用户资金明细表
class Finance_list_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->table = 'finance_list';
        $this->users_table = 'users';
        $this->users_info_table = 'users_info';
    }
    
    function get_list($where,$page,$limit){
        $this->db->select("*");
        $this->db->from($this->table);
        $this->db->where($where);
        $this->db->order_by('create_time','desc');
        $this->db->limit($limit, $page);
        return $this->db->get()->result_array();
    }
    
    function get_count($where){
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where($where);
        $this->db->order_by('create_time','desc');
        $query = $this->db->get();
        return $query->num_rows();
    }
    
    function getList($where,$page,$limit){
        $this->db->select($this->table.".*,".$this->users_table.'.username');
        $this->db->from($this->table);
        $this->db->join($this->users_table, $this->users_table.'.user_id = '.$this->table.'.friend_id', 'left');
        $this->db->where($where);
        $this->db->order_by('create_time','desc');
        $this->db->limit($limit, $page);
        return $this->db->get()->result_array();
    }
    
    function get_total_finance($where){
        $this->db->select('SUM(finance) finance');
        $this->db->from($this->table);
        $this->db->where($where);
        $this->db->order_by('id','desc');
        return $this->db->get()->row_array();
    }
    
     function getCount($where){
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where($where);
        $this->db->order_by('create_time','desc');
        $query = $this->db->get();
        return $query->num_rows();
    }
    
    function insert($data){
        $this->db->insert($this->table, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    //通过给定的时间段获取收入的溜溜币
    function get_in_count_by_time($where = array()){
        $this->db->select('SUM(finance) finance');
        $this->db->from($this->table);
        $this->db->where($where);
        $this->db->order_by('id','desc');
        return $this->db->get()->row_array();
    }
    
    //获取总推广收益
    function get_finance_total_count($where){
        $this->db->select('SUM(finance) finance');
        $this->db->from($this->table);
        $this->db->where($where);
        $this->db->order_by("create_time", "desc");
        $query = $this->db->get();
        $result = $query->row_array();
        return $result['finance'] ? $result['finance'] : 0;
    }
    
    //好友邀请奖励排行
    function getSpreadAmount($where = array(),$page,$limit){
        $this->db->select('SUM(finance) as total_award,'.$this->users_table.'.username,'.$this->users_info_table.".head_img,".$this->users_info_table.'.nickname');
        $this->db->from($this->table);
        $this->db->join($this->users_table, $this->users_table.'.user_id = '.$this->table.'.user_id', 'left');
        $this->db->join($this->users_info_table, $this->users_info_table.'.user_id = '.$this->table.'.user_id', 'left');
        $this->db->where($where);
        $this->db->group_by($this->table.'.user_id');
        $this->db->order_by("total_award desc");
        $this->db->limit($limit, $page);
        $query = $this->db->get();
        return $query->result_array();
    }
}
